<template>
  <div class="student-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa fa-user"></i> 学生管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <div class="query-form">
        <el-row :gutter="20">
          <el-col :span="2">
            <el-button @click="create" icon="el-icon-plus">新增学生</el-button>
          </el-col>

          <el-col :offset="15" :span="3">
            <el-input placeholder="学号" v-model="queryForm.username" />
          </el-col>

          <el-col :span="3">
            <el-button
              :disabled="!canSearch"
              @click="queryByUsername"
              icon="el-icon-search"
              type="primary"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </div>

      <el-row justify="center" type="flex">
        <el-pagination
          :current-page.sync="pageIndex"
          :page-size="pageSize"
          :total="pageSize * pageCount"
          @current-change="loadstudentInfo"
          background
          layout="prev, pager, next"
        >
        </el-pagination>
      </el-row>
      <div class="table" v-if="queryTableData.length > 0">
        <el-table :data="queryTableData" stripe>
          <el-table-column label="学生Id" prop="studentId" width="80px" />
          <el-table-column label="学号" prop="username" />
          <el-table-column label="姓名" prop="studentName" />
          <el-table-column label="密码" prop="password" />
          <el-table-column label="班级" prop="classId" />
          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                @click="edit(scope.row.studentId)"
                size="mini"
                type="success"
                >编辑
              </el-button>
              <el-button
                @click="deleteItem(scope.row.studentId)"
                size="mini"
                type="danger"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="table" v-else>
        <el-table :data="tableData" stripe>
          <el-table-column label="学生Id" prop="studentId" width="80px" />
          <el-table-column label="学号" prop="username" />
          <el-table-column label="姓名" prop="studentName" />
          <el-table-column label="密码" prop="password" />
          <el-table-column label="班级" prop="classId" />
          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                @click="edit(scope.row.studentId)"
                size="mini"
                type="success"
                >编辑
              </el-button>
              <el-button
                @click="deleteItem(scope.row.studentId)"
                size="mini"
                type="danger"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog :visible.sync="editing" title="编辑" width="30%">
        <el-form :model="entityForm" label-width="70px" ref="form">
          <el-form-item label="姓名">
            <el-input v-model="entityForm.studentName"></el-input>
          </el-form-item>
          <el-form-item label="学号">
            <el-input type="number" v-model="entityForm.username"></el-input>
          </el-form-item>
          <el-form-item label="所属班级">
            <el-select placeholder="请选择班级" v-model="entityForm.classId">
              <el-option
                :key="item.classId"
                :value="item.classId"
                :label="item.classId"
                v-for="item in classes"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="entityForm.password"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="save" type="primary">确 定</el-button>
          <el-button @click="editing = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import * as api from "../../api/admin/student";
import * as classApi from "../../api/admin/class";

export default {
  name: "AdminStudent",
  data() {
    return {
      queryForm: {
        username: "" // Add other query fields here if needed
      },
      entityForm: {
        classId: 20060141
        // ... other entityForm fields ...
      },
      tableData: [],
      queryTableData: [], // Separate data for query results
      pageSize: api.pageSize,
      pageCount: 1,
      pageIndex: 1,
      editing: false,
      classes: []
    };
  },
  computed: {
    canSearch() {
      return this.queryForm.username !== "" && !this.isSearching;
    }
  },

  methods: {
    query() {
      api.getPageCount().then(res => {
        this.pageCount = res;
        this.loadstudentInfo(this.pageIndex);
      });
    },
    queryByUsername() {
      api.getPageCount().then(res => {
        this.res = 1;
        this.pageCount = res;
        this.pageIndex = 1;
        this.queryStudentInfo(this.queryForm.username);
      });
    },
    queryStudentInfo(pageIndex) {
      api
        .getPageCountByStudentId(pageIndex, this.queryForm.username)
        .then(res => {
          this.queryTableData = res;
        });
    },
    loadstudentInfo(pageIndex) {
      api.studentInfo(pageIndex).then(res => {
        this.tableData = res;
      });
    },
    create() {
      this.entityForm = {
        studentId: -1,
        username: "",
        studentName: "",
        classId: "",
        password: ""
      };
      this.editing = true;
    },
    edit(studentId) {
      api.get(studentId).then(res => {
        this.entityForm = res;
        this.editing = true;
        this.loadstudentInfo(this.pageIndex);
      });
    },
    save() {
      if (this.entityForm.studentId === -1) {
        api.create(this.entityForm).then(() => {
          this.finishSave();
        });
      } else {
        api.update(this.entityForm).then(() => {
          this.finishSave();
        });
      }
    },
    finishSave() {
      this.$message.success("成功");
      this.editing = false;
      this.loadstudentInfo(this.pageIndex);
      this.query();
    },
    deleteItem(studentId) {
      api.deleteItem(studentId).then(() => {
        this.$message.success("删除成功");
        this.loadstudentInfo(this.pageIndex);
        this.query();
      });
    },
    getClasses() {
      classApi.listName().then(res => {
        this.classes = res;
      });
    }
  },
  created() {
    this.query();
    // this.loadstudentInfo(this.pageIndex);
    this.getClasses();
  }
};
</script>

<style scoped></style>
